package io.renren.modules.diycake.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.exception.RRException;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.diycake.entity.UserAccountEntity;
import io.renren.modules.diycake.service.UserAccountService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.diycake.entity.DiyUserEntity;
import io.renren.modules.diycake.service.UserService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author mohan
 * @email 1358665425@qq.com
 * @date 2022-01-09 16:40:26
 */
@RestController
@RequestMapping("diycake/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserAccountService userAccount;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("diycake:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("diycake:user:info")
    public R info(@PathVariable("id") Integer id){
		DiyUserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("diycake:user:save")
    public R save(@RequestBody DiyUserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("diycake:user:update")
    public R update(@RequestBody DiyUserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("diycake:user:delete")
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getAll")
    public R getAll() {
        return R.ok().put("list",userService.list(null));
    }


    @GetMapping("getUserById/{id}")
    @ApiOperation(value = "获取用户",notes = "获取用户根据id")
    public R getUserById(@PathVariable long id) {
        DiyUserEntity user = userService.getById(id);
        return R.ok().put("user",user);
    }



    @DeleteMapping("deleteUserById/{id}")
    @ApiOperation(value = "删除用户",notes = "删除用户")
    public R deleteUserById(@PathVariable Integer id) {
        boolean i = userService.removeById(id);
        return i? R.ok() : R.error();
    }


    @PostMapping("updatePassword/{id}")
    @ApiOperation(value = "更新密码",notes = "更新用户密码，三个字段必填")
    public R updatePassword(@PathVariable Integer id,
                            @RequestParam String oldPassword,
                            @RequestParam String newPassword){
        boolean i = userAccount.updatePassword(id,oldPassword,newPassword);
        return i? R.ok() : R.error();
    }

    @PostMapping("login")
    @ApiOperation(value = "登录，只需要phone和password，返回是Id",notes = "注册")
    public R loginUser(UserAccountEntity userAccount) {
        Integer token = userService.login(userAccount);
        return R.ok().put("id",token);
    }

    @PostMapping("register")
    @ApiOperation(value = "注册，只需要phone和password",notes = "注册")
    public R registerUser(UserAccountEntity userAccount) {
        boolean i = userService.register(userAccount);
        if (!i) {
            throw new RRException("注册用户失败");
        }
        return R.ok();
    }
}
